<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" :labelWidth="190"></FormPanel>
    <div style="height: 47px"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right',
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">保 存</multiuse-button>
    </div>

    <BaseDialog :visible.sync="isSearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <singleOwnerSeacher :rowData="dealerData" @close="ownerclose" />
    </BaseDialog>

    <BaseDialog :visible.sync="isProductSearch" :title="'物料选择'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <selectCar :rowData="ProductData" @close="ownerProductClose" />
    </BaseDialog>
  </div>
</template>
<script>
import { postSave, postSave1 } from '@/api/omSaleP/vehicleParameters/vehicleSpecialPrice/index.js';
import moment from 'moment';
import singleOwnerSeacher from './singleOwnerSeacher.vue';
import selectCar from './selectCar';
import { dictionary } from '@/utils/dictMixin';
import config from '@/assets/js/config';
export default {
  data() {
    this.saveData = null;
    return {
      uploadPrefix: config.uploadUrl, // 附件上传前缀
      fileList: [],
      formList: this.createdFrom(),
      tableList: [],
      dealerAddress: [],
      dealerAddressItemList: [],
      accountTypeList: [],
      isaddCar: false,
      dealerData: {},
      ProductData: {},
      json: {},
      isDiscount: false,
      DiscountData: {},
      TotalAmount: '',
      cache: {},
      ischange: false,
      isSearch: false,
      isProductSearch: false,
      rate: 0.5,
      redata: {},
    };
  },
  components: {
    singleOwnerSeacher,
    selectCar,
  },
  props: {
    rowData: {
      type: Object,
      default: {},
    },
  },
  created() {},
  mixins: [dictionary],
  mounted() {},
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          readonly: true,
          rules: [{ required: true, message: '请选择', trigger: 'blur' }],
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          },
          change: (val) => {
            this.clearFrom1(val);
          },
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          disabled: true,
        },
        {
          type: 'SELECT',
          label: '启用状态',
          fieldName: 'status',
          placeholder: '请选择',
          itemList: this.createDictList('1003'),
          initialValue: 10031001,
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '物料编码',
          fieldName: 'productCode',
          placeholder: '请选择',
          readonly: true,
          rules: [{ required: true, message: '请选择', trigger: 'blur' }],
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          },
          change: (val) => {
            this.clearFrom(val);
          },
        },
        {
          type: 'INPUT',
          label: '物料描述',
          fieldName: 'productName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '品牌',
          fieldName: 'brandName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '车系',
          fieldName: 'seriesName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '配置',
          fieldName: 'packageName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '选装',
          fieldName: 'optionName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '内饰',
          fieldName: 'trimName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '外饰',
          fieldName: 'colorName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT_NUMBER',
          precision: 2,
          label: '特殊采购价格',
          fieldName: 'specialPrice',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
        },
        {
          type: 'DATE',
          label: '有效开始时间',
          fieldName: 'startDate',
          placeholder: '请选择',
          dateType: 'exactdate',
          minDateTime: moment().format('YYYY-MM-DD'),
          initialValue: moment().format('YYYY-MM-DD'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
        },
        {
          type: 'DATE',
          label: '有效结束时间',
          fieldName: 'endDate',
          placeholder: '请选择',
          dateType: 'exactdate',
          minDateTime: moment().format('YYYY-MM-DD'),
          maxDateTime: moment().format('9999-12-31'),
          initialValue: moment().add(3, 'months').format('YYYY-MM-DD'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
        },
        {
          type: 'UPLOAD_FILE',
          label: '上传附件',
          fieldName: 'file',
          upload: {
            actionUrl: config.uploadUrl + '/api/file/oss/upload',
            fileTypes: ['jpg', 'png', 'pdf'],
            limit: 1,
            // tipText: '最多上传1个附件'
          },
          initialValue: this.rowData.fileName ? [{ name: this.rowData.fileName, url: this.rowData.fileAddress }] : [],
          selfCols: 2,
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          selfCols: 2,
          fieldName: 'remark',
          placeholder: '请输入',
        },
      ];
    },
    clearFrom1(val) {
      if (!val) {
        this.formList.find((x) => x.fieldName == 'dealerName').initialValue = '';
      }
    },
    clearFrom(val) {
      if (!val) {
        // this.$refs.from.RESET_FORM()
        this.formList.find((x) => x.fieldName == 'productCode').initialValue = '';
        this.formList.find((x) => x.fieldName == 'productName').initialValue = '';
        this.formList.find((x) => x.fieldName == 'brandName').initialValue = '';
        this.formList.find((x) => x.fieldName == 'seriesName').initialValue = '';
        this.formList.find((x) => x.fieldName == 'modelName').initialValue = '';
        this.formList.find((x) => x.fieldName == 'packageName').initialValue = '';
        this.formList.find((x) => x.fieldName == 'trimName').initialValue = '';
        this.formList.find((x) => x.fieldName == 'colorName').initialValue = '';
        this.formList.find((x) => x.fieldName == 'optionName').initialValue = '';
      }
    },
    seacherFun(type) {
      if (type == 1) {
        this.isSearch = true;
      } else {
        this.isProductSearch = true;
      }
    },
    ownerProductClose(data) {
      this.isProductSearch = false;
      if (data) {
        this.redata = data[0];
        this.formList.find((x) => x.fieldName == 'productCode').initialValue = this.redata.productCode;
        this.formList.find((x) => x.fieldName == 'productName').initialValue = this.redata.productName;
        this.formList.find((x) => x.fieldName == 'brandName').initialValue = this.redata.brandName;
        this.formList.find((x) => x.fieldName == 'seriesName').initialValue = this.redata.seriesName;
        this.formList.find((x) => x.fieldName == 'modelName').initialValue = this.redata.modelName;
        this.formList.find((x) => x.fieldName == 'packageName').initialValue = this.redata.configName;
        this.formList.find((x) => x.fieldName == 'trimName').initialValue = this.redata.trimColorName;
        this.formList.find((x) => x.fieldName == 'colorName').initialValue = this.redata.colorName;
        this.formList.find((x) => x.fieldName == 'optionName').initialValue = this.redata.optionName;
      }
    },
    // 上传成功回调
    successHandler(val) {},
    //经销商弹出框回调
    ownerclose(val, data) {
      if (data && data.length > 0) {
        let redata = data[0];
        // this.$refs.from.form = {...this.$refs.from.form, ...redata}
        // for (let i in redata) {
        //   this.formList.forEach((item) => {
        //     if (item.fieldName === i) {
        //       if (!(item.fieldName == 'status' && !redata[i])) {
        //         item.initialValue = redata[i];
        //       }
        //     }
        //   });
        // }
        this.formList.find((x) => x.fieldName == 'dealerCode').initialValue = redata.dealerCode;
        this.formList.find((x) => x.fieldName == 'dealerName').initialValue = redata.dealerShortname;
      }
      this.isSearch = false;
    },
    async getPicUrl(url) {
      let actionUrl = this.uploadPrefix + '/api/file/download/url?fileUrl=' + url;
      let res = await axios.post(actionUrl);
      return res.data;
    },
    changeHandle(val) {
      this.saveData = val;
    },
    async closeDrawer(type) {
      if (type == 1) {
        //关闭
        this.$emit('close');
      } else if (type == 2) {
        //保存或修改
        this.$refs.from.SUBMIT_FORM();
        const [err] = await this.$refs.from.GET_FORM_DATA();
        if (err) return;
        this.saveData = { ...this.saveData, ...this.redata };
        let json = JSON.parse(JSON.stringify(this.saveData));
        let res;
        res = await postSave(json);

        if (res.resultCode == 200) {
          this.$notify({
            title: '成功',
            message: '保存成功',
            type: 'success',
          });
          this.$emit('close', json);
        } else {
          this.saveData = null;
        }
      }
    },
  },
};
</script>
<style lang="less"></style>
